package com.example.hyg_veintype.activity.bleConnect2;

import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.example.hyg_veintype.R;
import com.example.hyg_veintype.data.local.analyseLocalBean.PluseAnalyseFuZhongChenWraper;
import com.example.hyg_veintype.data.local.analyseLocalBean.PluseDataWraper;
import com.example.hyg_veintype.activity.result.ResultActivity;
import com.example.hyg_veintype.base.BaseVeinTypeActivity;
import com.example.hyg_veintype.constant.ble.BleRssiDevice;
import com.example.hyg_veintype.constant.ble.PreBLEConnectedData;
import com.example.hyg_veintype.utils.chartUtil.RealTimeChartUtils2;
import com.example.pulseanalyse.PulseAnalyse;
import com.example.pulseanalyse.dealRealPointBLE.PointUtils2;
import com.example.pulseanalyse.veinPoint.myClassPluseAnalysisSD;
import com.github.mikephil.charting.charts.LineChart;
import com.hyg.lib_base.mainUtils.ToastUtils;


import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.ScheduledExecutorService;

import cn.com.heaton.blelibrary.ble.Ble;
import cn.com.heaton.blelibrary.ble.callback.BleConnectCallback;
import cn.com.heaton.blelibrary.ble.callback.BleNotifyCallback;
import cn.com.heaton.blelibrary.ble.callback.BleWriteCallback;
import cn.com.heaton.blelibrary.ble.model.BleDevice;
import cn.com.heaton.blelibrary.ble.utils.ByteUtils;
import cn.com.heaton.blelibrary.ble.utils.ThreadUtils;

/**
 * author: ${USER}
 * time: ${YEAR}/${MONTH}/${DAY}
 * desc: 蓝牙连接（脉型检测）页面
 */
public class ConnectBLE2Activity extends BaseVeinTypeActivity {
    public static final String BLETAG = "蓝牙====";
    public static final String JNITAG = "JNI====";
    public static final String EXTRA_TAG = "device";
    //写服务
    private UUID WriteUuidService;
    //写服务的特征值
    private UUID WriteUuidFeatureService;
    //通知服务
    private UUID NotifyUuidService;
    //通知服务的特征值
    private UUID NotifyUuidFeatureService;

    TextView tvBLEName;//蓝牙名称
    TextView tvBLEStatus;//蓝牙连接状态
    LineChart chartCun;
    LineChart chartGuan;
    LineChart chartChi;
    Button btStart;
    Button btEnd;
    Button btLeft;
    Button btRight;
    Button btAnalyse;

    //对外提供所有的蓝牙操作接口
    private Ble<BleDevice> ble;
    private BleDevice bleDevice;

    //蓝牙接收后的数据处理工具类
    PointUtils2 pointUtilsCun;
    PointUtils2 pointUtilsGuan;
    PointUtils2 pointUtilsChi;
    //蓝牙通知接口是否已初始化完成
    private volatile boolean hasNotifyInit = false;

    //JNI接口类
    PulseAnalyse pulseAnalyse = new PulseAnalyse();
    //JNI分析回调结果
    PulseAnalyse.PulseInfo pluseTypeCun1 = pulseAnalyse.new PulseInfo();
    PulseAnalyse.PulseInfo pluseTypeCun2 = pulseAnalyse.new PulseInfo();
    PulseAnalyse.PulseInfo pluseTypeCun3 = pulseAnalyse.new PulseInfo();
    PulseAnalyse.PulseInfo pluseTypeGuan1 = pulseAnalyse.new PulseInfo();
    PulseAnalyse.PulseInfo pluseTypeGuan2 = pulseAnalyse.new PulseInfo();
    PulseAnalyse.PulseInfo pluseTypeGuan3 = pulseAnalyse.new PulseInfo();
    PulseAnalyse.PulseInfo pluseTypeChi1 = pulseAnalyse.new PulseInfo();
    PulseAnalyse.PulseInfo pluseTypeChi2 = pulseAnalyse.new PulseInfo();
    PulseAnalyse.PulseInfo pluseTypeChi3 = pulseAnalyse.new PulseInfo();
    //接收线程
    volatile ScheduledExecutorService getDataService;
    //so方法分析中
    volatile boolean inAnalysising = true;

    RealTimeChartUtils2 realTimeChartCun;
    RealTimeChartUtils2 realTimeChartGuan;
    RealTimeChartUtils2 realTimeChartChi;

    //全面检测位置数据
    private List<PluseDataWraper> positionBeanList2 = new ArrayList<>();


    public static void start(Context context, BleRssiDevice device) {
        Intent starter = new Intent(context, ConnectBLE2Activity.class);
        starter.putExtra(ConnectBLE2Activity.EXTRA_TAG, device);
        context.startActivity(starter);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.veintype_activity_connect2;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initData();
        //初始化实时折线图
        realTimeChartCun = new RealTimeChartUtils2();
        realTimeChartGuan = new RealTimeChartUtils2();
        realTimeChartChi = new RealTimeChartUtils2();
        realTimeChartCun.setChart(chartCun, this, pointUtilsCun.drawPointY);
        realTimeChartGuan.setChart(chartGuan, this, pointUtilsGuan.drawPointY);
        realTimeChartChi.setChart(chartChi, this, pointUtilsChi.drawPointY);
    }

    private void initView() {
        tvBLEName = findViewById(R.id.tv_ble_name);//蓝牙名称
        tvBLEStatus = findViewById(R.id.tv_ble_status);
        chartCun = findViewById(R.id.chart_cun);
        chartGuan = findViewById(R.id.chart_guan);
        chartChi = findViewById(R.id.chart_chi);
        btStart = findViewById(R.id.bt_start);
        btStart.setOnClickListener(this);
        btEnd = findViewById(R.id.bt_end);
        btEnd.setOnClickListener(this);
        btLeft = findViewById(R.id.bt_left);
        btLeft.setOnClickListener(this);
        btRight = findViewById(R.id.bt_right);
        btRight.setOnClickListener(this);
        btAnalyse = findViewById(R.id.bt_analyse);
    }


    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.bt_start) {
            sendStartToBLE();
        } else if (id == R.id.bt_end) {
            sendStopToBLE();
//                LogFileUtils.writeTxtToFile(msg);
        } else if (id == R.id.bt_left) {
            switchHand(1);
        } else if (id == R.id.bt_right) {
            switchHand(2);
        } else if (id == R.id.bt_analyse) {
//                String str="[{\"fuZhongChenWraper\":{\"mainWindow\":{\"AvePulseRate\":80,\"AvePulseType\":\"\",\"PulseTypeChen\":\"\",\"PulseTypeFu\":\"\",\"PulseTypeZhong\":\"\",\"SynContext\":{},\"_newAnalysis\":{\"conditionList\":[{\"ConditionCount\":7,\"PulseType\":\"涩\"},{\"ConditionCount\":1,\"PulseType\":\"沉\"},{\"ConditionCount\":1,\"PulseType\":\"浮\"},{\"ConditionCount\":1,\"PulseType\":\"迟\"},{\"ConditionCount\":2,\"PulseType\":\"结\"},{\"ConditionCount\":2,\"PulseType\":\"代\"},{\"ConditionCount\":1,\"PulseType\":\"缓\"},{\"ConditionCount\":4,\"PulseType\":\"平\"},{\"ConditionCount\":7,\"PulseType\":\"实\"},{\"ConditionCount\":1,\"PulseType\":\"数\"},{\"ConditionCount\":2,\"PulseType\":\"促\"},{\"ConditionCount\":5,\"PulseType\":\"芤\"},{\"ConditionCount\":7,\"PulseType\":\"动\"},{\"ConditionCount\":1,\"PulseType\":\"疾\"},{\"ConditionCount\":9,\"PulseType\":\"紧\"},{\"ConditionCount\":7,\"PulseType\":\"濡\"},{\"ConditionCount\":2,\"PulseType\":\"洪\"},{\"ConditionCount\":6,\"PulseType\":\"牢\"},{\"ConditionCount\":4,\"PulseType\":\"弦\"},{\"ConditionCount\":5,\"PulseType\":\"滑\"},{\"ConditionCount\":2,\"PulseType\":\"虚\"},{\"ConditionCount\":2,\"PulseType\":\"伏\"},{\"ConditionCount\":5,\"PulseType\":\"革\"},{\"ConditionCount\":3,\"PulseType\":\"弱\"},{\"ConditionCount\":3,\"PulseType\":\"细\"},{\"ConditionCount\":1,\"PulseType\":\"微\"}]}}},\"isSelected\":false,\"positionName\":\"左-寸（心-小肠）\",\"simpleName\":\"左寸\",\"status\":0},{\"fuZhongChenWraper\":{\"mainWindow\":{\"AvePulseRate\":80,\"AvePulseType\":\"\",\"PulseTypeChen\":\"\",\"PulseTypeFu\":\"\",\"PulseTypeZhong\":\"\",\"SynContext\":{},\"_newAnalysis\":{\"conditionList\":[{\"ConditionCount\":7,\"PulseType\":\"涩\"},{\"ConditionCount\":1,\"PulseType\":\"沉\"},{\"ConditionCount\":1,\"PulseType\":\"浮\"},{\"ConditionCount\":1,\"PulseType\":\"迟\"},{\"ConditionCount\":2,\"PulseType\":\"结\"},{\"ConditionCount\":2,\"PulseType\":\"代\"},{\"ConditionCount\":1,\"PulseType\":\"缓\"},{\"ConditionCount\":4,\"PulseType\":\"平\"},{\"ConditionCount\":7,\"PulseType\":\"实\"},{\"ConditionCount\":1,\"PulseType\":\"数\"},{\"ConditionCount\":2,\"PulseType\":\"促\"},{\"ConditionCount\":5,\"PulseType\":\"芤\"},{\"ConditionCount\":7,\"PulseType\":\"动\"},{\"ConditionCount\":1,\"PulseType\":\"疾\"},{\"ConditionCount\":9,\"PulseType\":\"紧\"},{\"ConditionCount\":7,\"PulseType\":\"濡\"},{\"ConditionCount\":2,\"PulseType\":\"洪\"},{\"ConditionCount\":6,\"PulseType\":\"牢\"},{\"ConditionCount\":4,\"PulseType\":\"弦\"},{\"ConditionCount\":5,\"PulseType\":\"滑\"},{\"ConditionCount\":2,\"PulseType\":\"虚\"},{\"ConditionCount\":2,\"PulseType\":\"伏\"},{\"ConditionCount\":5,\"PulseType\":\"革\"},{\"ConditionCount\":3,\"PulseType\":\"弱\"},{\"ConditionCount\":3,\"PulseType\":\"细\"},{\"ConditionCount\":1,\"PulseType\":\"微\"}]}}},\"isSelected\":false,\"positionName\":\"右-寸（肺-大肠）\",\"simpleName\":\"右寸\",\"status\":0},{\"fuZhongChenWraper\":{\"chenSd\":{\"BBFIndex\":0,\"CBFIndex\":6,\"CBFIndexCopy\":6,\"CBGIndex\":0,\"CBGStartIndex\":25,\"_aListα\":[{\"x\":1,\"y\":55},{\"x\":3,\"y\":63}],\"_aListθ\":[{\"x\":3,\"y\":63},{\"x\":3,\"y\":63},{\"x\":3,\"y\":63},{\"x\":3,\"y\":63},{\"x\":3,\"y\":63},{\"x\":3,\"y\":63},{\"x\":3,\"y\":63},{\"x\":3,\"y\":63},{\"x\":3,\"y\":63},{\"x\":3,\"y\":63},{\"x\":3,\"y\":63},{\"x\":3,\"y\":63},{\"x\":3,\"y\":63},{\"x\":3,\"y\":63},{\"x\":3,\"y\":63},{\"x\":3,\"y\":63},{\"x\":3,\"y\":63},{\"x\":3,\"y\":63},{\"x\":3,\"y\":63},{\"x\":3,\"y\":63},{\"x\":3,\"y\":63},{\"x\":3,\"y\":63},{\"x\":3,\"y\":63},{\"x\":3,\"y\":63},{\"x\":3,\"y\":63},{\"x\":3,\"y\":63},{\"x\":5,\"y\":63},{\"x\":4,\"y\":64}],\"_bListα\":[{\"x\":3,\"y\":63},{\"x\":5,\"y\":63}],\"_bListθ\":[{\"x\":6,\"y\":61},{\"x\":6,\"y\":61},{\"x\":6,\"y\":61},{\"x\":6,\"y\":61},{\"x\":6,\"y\":61},{\"x\":6,\"y\":61},{\"x\":6,\"y\":61},{\"x\":6,\"y\":61},{\"x\":6,\"y\":61},{\"x\":6,\"y\":61},{\"x\":6,\"y\":61},{\"x\":6,\"y\":61},{\"x\":6,\"y\":61},{\"x\":6,\"y\":61},{\"x\":6,\"y\":61},{\"x\":6,\"y\":61},{\"x\":6,\"y\":61},{\"x\":6,\"y\":61},{\"x\":6,\"y\":61},{\"x\":6,\"y\":61},{\"x\":6,\"y\":61},{\"x\":6,\"y\":61},{\"x\":6,\"y\":61},{\"x\":6,\"y\":61},{\"x\":6,\"y\":61},{\"x\":6,\"y\":61},{\"x\":6,\"y\":61},{\"x\":6,\"y\":61}],\"_cListα\":[{\"x\":43,\"y\":55},{\"x\":43,\"y\":63}],\"_cListθ\":[{\"x\":7,\"y\":60},{\"x\":8,\"y\":58},{\"x\":9,\"y\":53},{\"x\":10,\"y\":49},{\"x\":11,\"y\":48},{\"x\":12,\"y\":45},{\"x\":13,\"y\":44},{\"x\":14,\"y\":42},{\"x\":15,\"y\":37},{\"x\":16,\"y\":36},{\"x\":17,\"y\":34},{\"x\":18,\"y\":34},{\"x\":19,\"y\":34},{\"x\":20,\"y\":36},{\"x\":21,\"y\":40},{\"x\":22,\"y\":51},{\"x\":23,\"y\":82},{\"x\":24,\"y\":111},{\"x\":25,\"y\":122},{\"x\":26,\"y\":118},{\"x\":27,\"y\":113},{\"x\":28,\"y\":107},{\"x\":29,\"y\":99},{\"x\":30,\"y\":91},{\"x\":31,\"y\":85},{\"x\":32,\"y\":78},{\"x\":16,\"y\":36},{\"x\":16,\"y\":36}],\"h3h1\":1.0,\"h4h1\":9.714285714285715,\"h5h1\":-9.714285714285715,\"intList\":[],\"listCBFStart\":[0,43],\"listCopy\":[54,55,58,63,64,63,61,60,58,53,49,48,45,44,42,37,36,34,34,34,36,40,51,82,111,122,118,113,107,99,91,85,78,67,61,59,60,62,63,61,59,55,54,52,52,51,50,51,52,48,48,46,45,46,47,51,64,92,114,120,110,100,91,83,74,70,63,57,56,58,64,69,70,69,66,61,57,57,54,51,50,48,42,40,39,39,41,43,52,80,111,123,123,115,110,103,94,87,81,69,61,58,56,59,60,61,59,54,52,51,48,50,51,51,49,46,46,48,46,46,45,55,86,115,126,121,110,102,97,91,84,76,69,65,64,68,70,73,73,73,69,64,60,57,55,53,49,45,44,43,42,41,41,46,55,84,113,124,123,118,112,104,96,90,82,68,59,57,57,58,59,60,58,56,53,51,52,50,49,46,46,48,50,45,42,43,45,49,82,107,116,114,108,96,86,79,76,69,59,53],\"myAutoEndIndex\":34,\"myAutoStartIndex\":0,\"newDataList\":[54,55,58,63,64,63,61,60,58,53,49,48,45,44,42,37,36,34,34,34,36,40,51,82,111,122,118,113,107,99,91,85,78,67,61,59,60,62,63,61,59,55,54,52,52,51,50,51,52,48,48,46,45,46,47,51,64,92,114,120,110,100,91,83,74,70,63,57,56,58,64,69,70,69,66,61,57,57,54,51,50,48,42,40,39,39,41,43,52,80,111,123,123,115,110,103,94,87,81,69,61,58,56,59,60,61,59,54,52,51,48,50,51,51,49,46,46,48,46,46,45,55,86,115,126,121,110,102,97,91,84,76,69,65,64,68,70,73,73,73,69,64,60,57,55,53,49,45,44,43,42,41,41,46,55,84,113,124,123,118,112,104,96,90,82,68,59,57,57,58,59,60,58,56,53,51,52,50,49,46,46,48,50,45,42,43,45,49,82,107,116,114,108,96,86,79,76,69,59,53],\"newDataListCopy\":[],\"pMax周期\":0.0,\"pMin周期\":10000.0,\"p主波上升期\":0.046875,\"p主波上部周期\":0.0,\"p主波入射角\":0.0,\"p主波夹角\":70.61045966596741,\"p主波幅高\":0.6979062811565304,\"p主波最大间隔\":0.0,\"p周期\":0.3359375,\"p周期方差\":0.0,\"p心率\":178.6046511627907,\"p是否间歇\":false,\"p潮波周期\":0.0,\"p潮波幅高\":0.6979062811565304,\"p脉力方差\":0.0,\"p重搏波周期\":0.3359375,\"p间歇是否规律\":false,\"t4\":0.0,\"t5\":0.3359375,\"testCBFList\":[],\"testList\":[43,43,43,43,43,43,43,43,44,45,46,48,50,52,55,58,60,63,66,68,71,73,75,77,78,79,80,80,80,79,78,77,76,75,73,72,70,69,68,67,66,65,65,65,65,66,66,67,69,70,71,73,74,75,77,78,79,80,80,80,80,80,79,79,78,77,76,75,74,73,72,71,70,69,69,69,69,70,71,72,74,75,77,79,80,82,83,85,86,87,87,87,87,87,86,85,84,82,81,79,77,75,74,72,71,70,69,69,69,69,70,71,72,73,75,77,79,81,83,85,87,88,90,91,92,92,92,92,92,91,90,89,88,86,85,83,82,80,79,78,77,77,77,77,78,79,80,81,82,84,85,86,88,89,90,90,90,90,90,89,88,87,85,84,82,80,78,76,74,72,70,68,67,66,66,65,65,66,66,67,69,70,72,74,76,78,80,81,83,85,86,88,89,90,91,91,92,92,92,92],\"单峰List\":[],\"双峰List\":[],\"多峰List\":[],\"重搏波与降中峡之差h5\":-6.779661016949153,\"降中峡h4\":6.779661016949153},\"fuSd\":{\"BBFIndex\":80,\"CBFIndex\":80,\"CBFIndexCopy\":80,\"CBGIndex\":0,\"CBGStartIndex\":0,\"_aListα\":[{\"x\":78,\"y\":51},{\"x\":80,\"y\":49},{\"x\":76,\"y\":54},{\"x\":72,\"y\":63}],\"_aListθ\":[{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49}],\"_bListα\":[{\"x\":80,\"y\":49},{\"x\":82,\"y\":48},{\"x\":80,\"y\":49},{\"x\":74,\"y\":57}],\"_bListθ\":[{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":80,\"y\":49},{\"x\":85,\"y\":46},{\"x\":85,\"y\":46},{\"x\":85,\"y\":46},{\"x\":85,\"y\":46},{\"x\":85,\"y\":46},{\"x\":85,\"y\":46},{\"x\":85,\"y\":46},{\"x\":85,\"y\":46},{\"x\":85,\"y\":46},{\"x\":85,\"y\":46},{\"x\":85,\"y\":46},{\"x\":85,\"y\":46},{\"x\":85,\"y\":46},{\"x\":85,\"y\":46},{\"x\":85,\"y\":46},{\"x\":85,\"y\":46},{\"x\":85,\"y\":46},{\"x\":85,\"y\":46},{\"x\":85,\"y\":46},{\"x\":85,\"y\":46},{\"x\":85,\"y\":46},{\"x\":85,\"y\":46}],\"_cListα\":[{\"x\":112,\"y\":51},{\"x\":112,\"y\":49},{\"x\":112,\"y\":54},{\"x\":112,\"y\":63}],\"_cListθ\":[{\"x\":81,\"y\":48},{\"x\":82,\"y\":48},{\"x\":83,\"y\":47},{\"x\":84,\"y\":47},{\"x\":85,\"y\":46},{\"x\":86,\"y\":48},{\"x\":87,\"y\":50},{\"x\":88,\"y\":60},{\"x\":89,\"y\":73},{\"x\":90,\"y\":91},{\"x\":91,\"y\":105},{\"x\":92,\"y\":107},{\"x\":93,\"y\":102},{\"x\":94,\"y\":96},{\"x\":95,\"y\":89},{\"x\":96,\"y\":81},{\"x\":97,\"y\":75},{\"x\":98,\"y\":68},{\"x\":99,\"y\":63},{\"x\":100,\"y\":59},{\"x\":101,\"y\":55},{\"x\":102,\"y\":57},{\"x\":103,\"y\":59},{\"x\":104,\"y\":61},{\"x\":105,\"y\":60},{\"x\":106,\"y\":59},{\"x\":107,\"y\":54},{\"x\":86,\"y\":48},{\"x\":87,\"y\":50},{\"x\":88,\"y\":60},{\"x\":89,\"y\":73},{\"x\":90,\"y\":91},{\"x\":91,\"y\":105},{\"x\":92,\"y\":107},{\"x\":93,\"y\":102},{\"x\":94,\"y\":96},{\"x\":95,\"y\":89},{\"x\":96,\"y\":81},{\"x\":97,\"y\":75},{\"x\":98,\"y\":68},{\"x\":99,\"y\":63},{\"x\":100,\"y\":59},{\"x\":101,\"y\":55},{\"x\":102,\"y\":57},{\"x\":103,\"y\":59},{\"x\":104,\"y\":61},{\"x\":105,\"y\":60},{\"x\":106,\"y\":59},{\"x\":107,\"y\":54}],\"h3h1\":1.0,\"h4h1\":4.0,\"h5h1\":0.0,\"intList\":[],\"listCBFStart\":[81,112],\"listCopy\":[52,51,50,49,50,51,52,52,51,48,47,46,46,47,50,65,83,99,105,103,93,86,79,76,72,67,65,60,63,64,64,64,63,61,60,58,57,53,52,52,51,51,51,50,50,50,51,49,49,50,53,58,71,89,102,107,102,93,84,76,71,63,57,52,52,54,59,62,61,63,63,63,63,61,57,55,54,52,51,51,49,48,48,47,47,46,48,50,60,73,91,105,107,102,96,89,81,75,68,63,59,55,57,59,61,60,59,54,52,51,49,49,48,49,51,53,51,50,48,48,45,45,46,48,57,74,90,100,102,96,90,82,77,73,69,66,64,63,63,64,64,64,63,60,58,56,53,52,49,48,47,47,48,48,49,48,49,48,49,49,49,49,55,69,87,103,105,102,94,84,76,72,67,63,59,55,56,59,61,64,64,66,64,62,60,56,52,51,49,49,49,48,47,49,47,46,47,46,48,49],\"myAutoEndIndex\":109,\"myAutoStartIndex\":73,\"newDataList\":[52,51,50,49,50,51,52,52,51,48,47,46,46,47,50,65,83,99,105,103,93,86,79,76,72,67,65,60,63,64,64,64,63,61,60,58,57,53,52,52,51,51,51,50,50,50,51,49,49,50,53,58,71,89,102,107,102,93,84,76,71,63,57,52,52,54,59,62,61,63,63,63,63,61,57,55,54,52,51,51,49,48,48,47,47,46,48,50,60,73,91,105,107,102,96,89,81,75,68,63,59,55,57,59,61,60,59,54,52,51,49,49,48,49,51,53,51,50,48,48,45,45,46,48,57,74,90,100,102,96,90,82,77,73,69,66,64,63,63,64,64,64,63,60,58,56,53,52,49,48,47,47,48,48,49,48,49,48,49,49,49,49,55,69,87,103,105,102,94,84,76,72,67,63,59,55,56,59,61,64,64,66,64,62,60,56,52,51,49,49,49,48,47,49,47,46,47,46,48,49],\"newDataListCopy\":[],\"pMax周期\":0.0,\"pMin周期\":10000.0,\"p主波上升期\":-0.0078125,\"p主波上部周期\":0.0,\"p主波入射角\":71.56505117708012,\"p主波夹角\":61.84297037345999,\"p主波幅高\":0.09970089730807578,\"p主波最大间隔\":0.0,\"p周期\":0.2421875,\"p周期方差\":0.0,\"p心率\":247.74193548387098,\"p是否间歇\":false,\"p潮波周期\":-0.0078125,\"p潮波幅高\":0.09970089730807578,\"p脉力方差\":0.0,\"p重搏波周期\":0.2421875,\"p间歇是否规律\":false,\"t4\":0.0,\"t5\":0.875,\"testCBFList\":[],\"testList\":[32,33,34,35,37,39,41,44,47,50,53,56,59,62,64,66,68,70,71,72,72,72,72,71,70,68,67,65,63,61,59,57,56,54,53,52,51,51,51,51,52,53,54,56,58,60,62,63,65,67,68,69,70,71,71,71,71,70,69,68,67,65,64,62,60,58,56,54,53,51,50,49,48,48,48,48,49,50,51,53,55,57,59,61,64,66,68,69,70,71,72,72,72,72,71,70,68,66,64,62,59,57,54,52,50,48,46,45,44,43,43,43,43,44,45,47,49,51,53,55,58,60,62,65,66,68,69,70,71,71,71,70,69,68,66,64,62,60,58,56,53,51,49,47,45,44,43,42,42,42,42,43,45,46,48,50,53,55,58,60,62,65,66,68,69,70,71,71,71,71,70,69,67,65,63,61,59,56,54,51,48,45,42,39,36,33,30,28,25,23,21,19,17,15,14,12,11,11,10,10],\"单峰List\":[0],\"双峰List\":[],\"多峰List\":[],\"重搏波与降中峡之差h5\":0.0,\"降中峡h4\":0.39880358923230314},\"mainWindow\":{\"AvePulseRate\":80,\"AvePulseType\":\"\",\"PulseTypeChen\":\"\",\"PulseTypeFu\":\"\",\"PulseTypeZhong\":\"\",\"SynContext\":{},\"_newAnalysis\":{\"conditionList\":[{\"ConditionCount\":7,\"PulseType\":\"涩\"},{\"ConditionCount\":1,\"PulseType\":\"沉\"},{\"ConditionCount\":1,\"PulseType\":\"浮\"},{\"ConditionCount\":1,\"PulseType\":\"迟\"},{\"ConditionCount\":2,\"PulseType\":\"结\"},{\"ConditionCount\":2,\"PulseType\":\"代\"},{\"ConditionCount\":1,\"PulseType\":\"缓\"},{\"ConditionCount\":4,\"PulseType\":\"平\"},{\"ConditionCount\":7,\"PulseType\":\"实\"},{\"ConditionCount\":1,\"PulseType\":\"数\"},{\"ConditionCount\":2,\"PulseType\":\"促\"},{\"ConditionCount\":5,\"PulseType\":\"芤\"},{\"ConditionCount\":7,\"PulseType\":\"动\"},{\"ConditionCount\":1,\"PulseType\":\"疾\"},{\"ConditionCount\":9,\"PulseType\":\"紧\"},{\"ConditionCount\":7,\"PulseType\":\"濡\"},{\"ConditionCount\":2,\"PulseType\":\"洪\"},{\"ConditionCount\":6,\"PulseType\":\"牢\"},{\"ConditionCount\":4,\"PulseType\":\"弦\"},{\"ConditionCount\":5,\"PulseType\":\"滑\"},{\"ConditionCount\":2,\"PulseType\":\"虚\"},{\"ConditionCount\":2,\"PulseType\":\"伏\"},{\"ConditionCount\":5,\"PulseType\":\"革\"},{\"ConditionCount\":3,\"PulseType\":\"弱\"},{\"ConditionCount\":3,\"PulseType\":\"细\"},{\"ConditionCount\":1,\"PulseType\":\"微\"}]}}},\"isSelected\":false,\"positionName\":\"左-关（肝-胆）\",\"simpleName\":\"左关\",\"status\":0},{\"fuZhongChenWraper\":{\"mainWindow\":{\"AvePulseRate\":80,\"AvePulseType\":\"\",\"PulseTypeChen\":\"\",\"PulseTypeFu\":\"\",\"PulseTypeZhong\":\"\",\"SynContext\":{},\"_newAnalysis\":{\"conditionList\":[{\"ConditionCount\":7,\"PulseType\":\"涩\"},{\"ConditionCount\":1,\"PulseType\":\"沉\"},{\"ConditionCount\":1,\"PulseType\":\"浮\"},{\"ConditionCount\":1,\"PulseType\":\"迟\"},{\"ConditionCount\":2,\"PulseType\":\"结\"},{\"ConditionCount\":2,\"PulseType\":\"代\"},{\"ConditionCount\":1,\"PulseType\":\"缓\"},{\"ConditionCount\":4,\"PulseType\":\"平\"},{\"ConditionCount\":7,\"PulseType\":\"实\"},{\"ConditionCount\":1,\"PulseType\":\"数\"},{\"ConditionCount\":2,\"PulseType\":\"促\"},{\"ConditionCount\":5,\"PulseType\":\"芤\"},{\"ConditionCount\":7,\"PulseType\":\"动\"},{\"ConditionCount\":1,\"PulseType\":\"疾\"},{\"ConditionCount\":9,\"PulseType\":\"紧\"},{\"ConditionCount\":7,\"PulseType\":\"濡\"},{\"ConditionCount\":2,\"PulseType\":\"洪\"},{\"ConditionCount\":6,\"PulseType\":\"牢\"},{\"ConditionCount\":4,\"PulseType\":\"弦\"},{\"ConditionCount\":5,\"PulseType\":\"滑\"},{\"ConditionCount\":2,\"PulseType\":\"虚\"},{\"ConditionCount\":2,\"PulseType\":\"伏\"},{\"ConditionCount\":5,\"PulseType\":\"革\"},{\"ConditionCount\":3,\"PulseType\":\"弱\"},{\"ConditionCount\":3,\"PulseType\":\"细\"},{\"ConditionCount\":1,\"PulseType\":\"微\"}]}}},\"isSelected\":false,\"positionName\":\"右-关（脾-胃）\",\"simpleName\":\"右关\",\"status\":0},{\"fuZhongChenWraper\":{\"mainWindow\":{\"AvePulseRate\":80,\"AvePulseType\":\"\",\"PulseTypeChen\":\"\",\"PulseTypeFu\":\"\",\"PulseTypeZhong\":\"\",\"SynContext\":{},\"_newAnalysis\":{\"conditionList\":[{\"ConditionCount\":7,\"PulseType\":\"涩\"},{\"ConditionCount\":1,\"PulseType\":\"沉\"},{\"ConditionCount\":1,\"PulseType\":\"浮\"},{\"ConditionCount\":1,\"PulseType\":\"迟\"},{\"ConditionCount\":2,\"PulseType\":\"结\"},{\"ConditionCount\":2,\"PulseType\":\"代\"},{\"ConditionCount\":1,\"PulseType\":\"缓\"},{\"ConditionCount\":4,\"PulseType\":\"平\"},{\"ConditionCount\":7,\"PulseType\":\"实\"},{\"ConditionCount\":1,\"PulseType\":\"数\"},{\"ConditionCount\":2,\"PulseType\":\"促\"},{\"ConditionCount\":5,\"PulseType\":\"芤\"},{\"ConditionCount\":7,\"PulseType\":\"动\"},{\"ConditionCount\":1,\"PulseType\":\"疾\"},{\"ConditionCount\":9,\"PulseType\":\"紧\"},{\"ConditionCount\":7,\"PulseType\":\"濡\"},{\"ConditionCount\":2,\"PulseType\":\"洪\"},{\"ConditionCount\":6,\"PulseType\":\"牢\"},{\"ConditionCount\":4,\"PulseType\":\"弦\"},{\"ConditionCount\":5,\"PulseType\":\"滑\"},{\"ConditionCount\":2,\"PulseType\":\"虚\"},{\"ConditionCount\":2,\"PulseType\":\"伏\"},{\"ConditionCount\":5,\"PulseType\":\"革\"},{\"ConditionCount\":3,\"PulseType\":\"弱\"},{\"ConditionCount\":3,\"PulseType\":\"细\"},{\"ConditionCount\":1,\"PulseType\":\"微\"}]}}},\"isSelected\":false,\"positionName\":\"左-尺（肾-膀胱）\",\"simpleName\":\"左尺\",\"status\":0},{\"fuZhongChenWraper\":{\"mainWindow\":{\"AvePulseRate\":80,\"AvePulseType\":\"\",\"PulseTypeChen\":\"\",\"PulseTypeFu\":\"\",\"PulseTypeZhong\":\"\",\"SynContext\":{},\"_newAnalysis\":{\"conditionList\":[{\"ConditionCount\":7,\"PulseType\":\"涩\"},{\"ConditionCount\":1,\"PulseType\":\"沉\"},{\"ConditionCount\":1,\"PulseType\":\"浮\"},{\"ConditionCount\":1,\"PulseType\":\"迟\"},{\"ConditionCount\":2,\"PulseType\":\"结\"},{\"ConditionCount\":2,\"PulseType\":\"代\"},{\"ConditionCount\":1,\"PulseType\":\"缓\"},{\"ConditionCount\":4,\"PulseType\":\"平\"},{\"ConditionCount\":7,\"PulseType\":\"实\"},{\"ConditionCount\":1,\"PulseType\":\"数\"},{\"ConditionCount\":2,\"PulseType\":\"促\"},{\"ConditionCount\":5,\"PulseType\":\"芤\"},{\"ConditionCount\":7,\"PulseType\":\"动\"},{\"ConditionCount\":1,\"PulseType\":\"疾\"},{\"ConditionCount\":9,\"PulseType\":\"紧\"},{\"ConditionCount\":7,\"PulseType\":\"濡\"},{\"ConditionCount\":2,\"PulseType\":\"洪\"},{\"ConditionCount\":6,\"PulseType\":\"牢\"},{\"ConditionCount\":4,\"PulseType\":\"弦\"},{\"ConditionCount\":5,\"PulseType\":\"滑\"},{\"ConditionCount\":2,\"PulseType\":\"虚\"},{\"ConditionCount\":2,\"PulseType\":\"伏\"},{\"ConditionCount\":5,\"PulseType\":\"革\"},{\"ConditionCount\":3,\"PulseType\":\"弱\"},{\"ConditionCount\":3,\"PulseType\":\"细\"},{\"ConditionCount\":1,\"PulseType\":\"微\"}]}}},\"isSelected\":false,\"positionName\":\"右-尺（肾-命门）\",\"simpleName\":\"右尺\",\"status\":0}]";
//                Type type = new TypeToken<List<PluseDataWraper>>() {}.getType();
//                Gson gson=new Gson();
//                positionBeanList2=gson.fromJson(str,type);
//                positionBeanList2.get(2).setStatus(2);
//                positionBeanList2.get(1).setFuZhongChenWraper(positionBeanList2.get(2).getFuZhongChenWraper());
//                positionBeanList2.get(1).setStatus(2);
            analyse();
            ResultActivity.start(this, positionBeanList2);
        }
    }


    private void analyse() {
        if (mark_StartEnd == 2 && mark_leftRight == 1) {
            useJNI(pointUtilsCun.fuData, pluseTypeCun1, 1, 1, 1);
            useJNI(pointUtilsCun.zhongData, pluseTypeCun2, 1, 1, 2);
            useJNI(pointUtilsCun.chenData, pluseTypeCun3, 1, 1, 3);
            useJNI(pointUtilsGuan.fuData, pluseTypeGuan1, 1, 2, 1);
            useJNI(pointUtilsGuan.zhongData, pluseTypeGuan2, 1, 2, 2);
            useJNI(pointUtilsGuan.chenData, pluseTypeGuan3, 1, 2, 3);
            useJNI(pointUtilsChi.fuData, pluseTypeChi1, 1, 3, 1);
            useJNI(pointUtilsChi.zhongData, pluseTypeChi2, 1, 3, 2);
            useJNI(pointUtilsChi.chenData, pluseTypeChi3, 1, 3, 3);
        } else if (mark_StartEnd == 2 && mark_leftRight == 2) {
            useJNI(pointUtilsCun.fuData, pluseTypeCun1, 2, 1, 1);
            useJNI(pointUtilsCun.zhongData, pluseTypeCun2, 2, 1, 2);
            useJNI(pointUtilsCun.chenData, pluseTypeCun3, 2, 1, 3);
            useJNI(pointUtilsGuan.fuData, pluseTypeGuan1, 2, 2, 1);
            useJNI(pointUtilsGuan.zhongData, pluseTypeGuan2, 2, 2, 2);
            useJNI(pointUtilsGuan.chenData, pluseTypeGuan3, 2, 2, 3);
            useJNI(pointUtilsChi.fuData, pluseTypeChi1, 2, 3, 1);
            useJNI(pointUtilsChi.zhongData, pluseTypeChi2, 2, 3, 2);
            useJNI(pointUtilsChi.chenData, pluseTypeChi3, 2, 3, 3);
        }

    }

    /**
     * 调用张帅的JNI获取脉的集合，并调用本地算法分析脉点
     *
     * @param list            蓝牙获取的数据
     * @param pluseType       张帅实体类
     * @param zuoYouMark      左右手标志符 1，左手     2，右手
     * @param cunGuanChiMark  寸关尺标识符 1，寸     2，关     3，尺
     * @param fuZhongChenMark 浮中沉标识符 1，浮     2，中      3沉
     */
    private void useJNI(LinkedList<Integer> list, PulseAnalyse.PulseInfo pluseType, int zuoYouMark, int cunGuanChiMark, int fuZhongChenMark) {
        try {
            int result = -1;
            if (list.size() > 0) {
                result = pulseAnalyse.ManualAnalysisJNI(listToArray(list), pluseType);
            }
            //从张帅so文件得到了分析结果，下面是脉点分析处理
            if (result != -1) {
                List<Integer> bestList = new ArrayList<>();
                int[] getData = pluseType.getData();
                for (int i = 0; i < getData.length; i++) {
                    bestList.add(getData[i]);
                }
                myClassPluseAnalysisSD sd = new myClassPluseAnalysisSD(list, pluseType.getBegin(), pluseType.getEnd(), pluseType.getBegin() + bestList.indexOf(Collections.max(bestList)));
                sd.setPixelsPerSecond50();
                if (zuoYouMark == 1) {
                    if (cunGuanChiMark == 1) {
                        //左寸
                        PluseAnalyseFuZhongChenWraper zuoCun = positionBeanList2.get(0).getFuZhongChenWraper();
                        positionBeanList2.get(0).setStatus(2);
                        if (fuZhongChenMark == 1) {
                            //浮
                            zuoCun.setFuSd(sd);
                        } else if (fuZhongChenMark == 2) {
                            //中
                            zuoCun.setZhongSd(sd);
                        } else if (fuZhongChenMark == 3) {
                            //沉
                            zuoCun.setChenSd(sd);
                        }
                    } else if (cunGuanChiMark == 2) {
                        //左关
                        PluseAnalyseFuZhongChenWraper zuoGuan = positionBeanList2.get(2).getFuZhongChenWraper();
                        positionBeanList2.get(2).setStatus(2);
                        if (fuZhongChenMark == 1) {
                            //浮
                            zuoGuan.setFuSd(sd);
                        } else if (fuZhongChenMark == 2) {
                            //中
                            zuoGuan.setZhongSd(sd);
                        } else if (fuZhongChenMark == 3) {
                            //沉
                            zuoGuan.setChenSd(sd);
                        }
                    } else if (cunGuanChiMark == 3) {
                        //左尺
                        PluseAnalyseFuZhongChenWraper zuoChi = positionBeanList2.get(4).getFuZhongChenWraper();
                        positionBeanList2.get(4).setStatus(2);
                        if (fuZhongChenMark == 1) {
                            //浮
                            zuoChi.setFuSd(sd);
                        } else if (fuZhongChenMark == 2) {
                            //中
                            zuoChi.setZhongSd(sd);
                        } else if (fuZhongChenMark == 3) {
                            //沉
                            zuoChi.setChenSd(sd);
                        }
                    }
                } else {
                    if (cunGuanChiMark == 1) {
                        //右寸
                        PluseAnalyseFuZhongChenWraper youCun = positionBeanList2.get(1).getFuZhongChenWraper();
                        positionBeanList2.get(1).setStatus(2);
                        if (fuZhongChenMark == 1) {
                            //浮
                            youCun.setFuSd(sd);
                        } else if (fuZhongChenMark == 2) {
                            //中
                            youCun.setZhongSd(sd);
                        } else if (fuZhongChenMark == 3) {
                            //沉
                            youCun.setChenSd(sd);
                        }
                    } else if (cunGuanChiMark == 2) {
                        //右关
                        PluseAnalyseFuZhongChenWraper youGuan = positionBeanList2.get(3).getFuZhongChenWraper();
                        positionBeanList2.get(3).setStatus(2);
                        if (fuZhongChenMark == 1) {
                            //浮
                            youGuan.setFuSd(sd);
                        } else if (fuZhongChenMark == 2) {
                            //中
                            youGuan.setZhongSd(sd);
                        } else if (fuZhongChenMark == 3) {
                            //沉
                            youGuan.setChenSd(sd);
                        }
                    } else if (cunGuanChiMark == 3) {
                        //右尺
                        PluseAnalyseFuZhongChenWraper youChi = positionBeanList2.get(5).getFuZhongChenWraper();
                        positionBeanList2.get(5).setStatus(2);
                        if (fuZhongChenMark == 1) {
                            //浮
                            youChi.setFuSd(sd);
                        } else if (fuZhongChenMark == 2) {
                            //中
                            youChi.setZhongSd(sd);
                        } else if (fuZhongChenMark == 3) {
                            //沉
                            youChi.setChenSd(sd);
                        }
                    }
                }
            }
        } catch (Exception e) {
            int i = 0;
        }
    }

    /**
     * 初始化蓝牙服务及特征值服务并连接蓝牙设备
     */
    private void initData() {
        pointUtilsCun = new PointUtils2();
        pointUtilsGuan = new PointUtils2();
        pointUtilsChi = new PointUtils2();

        positionBeanList2.add(new PluseDataWraper("左寸", "左-寸（心-小肠）", false, 0, new PluseAnalyseFuZhongChenWraper()));
        positionBeanList2.add(new PluseDataWraper("右寸", "右-寸（肺-大肠）", false, 0, new PluseAnalyseFuZhongChenWraper()));
        positionBeanList2.add(new PluseDataWraper("左关", "左-关（肝-胆）", false, 0, new PluseAnalyseFuZhongChenWraper()));
        positionBeanList2.add(new PluseDataWraper("右关", "右-关（脾-胃）", false, 0, new PluseAnalyseFuZhongChenWraper()));
        positionBeanList2.add(new PluseDataWraper("左尺", "左-尺（肾-膀胱）", false, 0, new PluseAnalyseFuZhongChenWraper()));
        positionBeanList2.add(new PluseDataWraper("右尺", "右-尺（肾-命门）", false, 0, new PluseAnalyseFuZhongChenWraper()));

        WriteUuidService = UUID.fromString("49535343-FE7D-4AE5-8FA9-9FAFD205E455");
        WriteUuidFeatureService = UUID.fromString("49535343-8841-43F4-A8D4-ECBE34729BB3");
        NotifyUuidService = UUID.fromString("49535343-FE7D-4AE5-8FA9-9FAFD205E455");
        NotifyUuidFeatureService = UUID.fromString("49535343-1E4D-4BD9-BA61-23C647249616");
        ble = Ble.getInstance();
        bleDevice = getIntent().getParcelableExtra(EXTRA_TAG);
        tvTitle.setText("脉象采集");
        if (!TextUtils.isEmpty(bleDevice.getBleName())) {
            tvBLEName.setText(bleDevice.getBleName());
        } else {
            tvBLEName.setText("未知设备");
        }
        ble.connect(bleDevice, connectCallback);
    }

    /**
     * 切换左右手
     *
     * @param index 1，左手 2，右手
     */
    private void switchHand(int index) {
        sendStopToBLE();
        byte[] bytes = null;
        if (index == 1) {
            //左手
            bytes = ByteUtils.hexStr2Bytes("AB01");
            mark_leftRight = 1;
        } else {
            //右手
            bytes = ByteUtils.hexStr2Bytes("AB02");
            mark_leftRight = 2;
        }
        writeChar(bleDevice, bytes);
    }

    /**
     * 向蓝牙发送开启指令
     */
    private void sendStartToBLE() {
        pointUtilsCun = new PointUtils2();
        pointUtilsGuan = new PointUtils2();
        pointUtilsChi = new PointUtils2();
        if (!hasNotifyInit) {
            ToastUtils.showShort("蓝牙通知服务初始化中...");
            return;
        }
        mark_StartEnd = 1;
        //开启
        byte[] bytes = ByteUtils.hexStr2Bytes("FA00");//纯数返回
//        byte[] bytes = ByteUtils.hexStr2Bytes("FA0B");//字符串返回
        writeChar(bleDevice, bytes);
    }

    /**
     * 向蓝牙发送关闭指令
     */
    private void sendStopToBLE() {
        //关闭
        byte[] bytes = ByteUtils.hexStr2Bytes("FB00");
        writeChar(bleDevice, bytes);
    }

    /**
     * 开启通知服务通知
     *
     * @param turn 是否开启通知服务
     */
    private void enableNotifyByUuid(boolean turn) {
        Ble.getInstance().enableNotifyByUuid(
                bleDevice,
                turn,
                NotifyUuidService,
                NotifyUuidFeatureService,
                notitifyCallback);
    }

    String msg = "";
    int positionIndex = -1;//1、2总按（寸关尺一块采集）开关，3、4寸开关， 5、6关开关， 7、8尺开关
    int mark_StartEnd = 1;//1代表开始，2代表正常结束
    int mark_leftRight = 1;//1代表左手，2代表右手
    private String mark_all1 = "b06101";//总取浮数据标记
    private String mark_all2 = "b06103";//总取中数据标记
    private String mark_all3 = "b06105";//总取沉数据标记
    private String mark_cun1 = "b06201";//寸取浮数据标记
    private String mark_cun2 = "b06203";//寸取中数据标记
    private String mark_cun3 = "b06205";//寸取沉数据标记
    private String mark_guan1 = "b06301";//关取浮数据标记
    private String mark_guan2 = "b06303";//关取中数据标记
    private String mark_guan3 = "b06305";//关取沉数据标记
    private String mark_chi1 = "b06401";//尺取浮数据标记
    private String mark_chi2 = "b06403";//尺取中数据标记
    private String mark_chi3 = "b06405";//尺取沉数据标记

    /**
     * 蓝牙通知回调
     */
    BleNotifyCallback<BleDevice> notitifyCallback = new BleNotifyCallback<BleDevice>() {
        @Override
        public void onChanged(BleDevice device, final BluetoothGattCharacteristic characteristic) {
            try {
                UUID uuid = characteristic.getUuid();
                if (!uuid.toString().equals(NotifyUuidFeatureService.toString())) {
                    return;
                }
                byte[] bytes = characteristic.getValue();
                String getByteStr = ByteUtils.bytes2HexStr(bytes);
                msg = msg + getByteStr + "\n";
                Log.d("getByte[]===", getByteStr);

                BigDecimal bigDecimal1 = new BigDecimal("50");
                BigDecimal bigDecimal2 = new BigDecimal("30");
                BigDecimal modulus = bigDecimal2.divide(bigDecimal1, 2, BigDecimal.ROUND_HALF_UP);

//                //开始总按
//                if ("b021000000000000000000000000000000000000".equals(getByteStr)) {
//                    positionIndex = 1;
//                }
//                //结束总按
//                if ("b022000000000000000000000000000000000000".equals(getByteStr)) {
//                    positionIndex = 2;
//                }
//                //开始寸按
//                if ("b023000000000000000000000000000000000000".equals(getByteStr)) {
//                    positionIndex = 3;
//                }
//                //结束寸按
//                if ("b024000000000000000000000000000000000000".equals(getByteStr)) {
//                    positionIndex = 4;
//                }
//                //开始关按
//                if ("b025000000000000000000000000000000000000".equals(getByteStr)) {
//                    positionIndex = 5;
//                }
//                //结束关按
//                if ("b026000000000000000000000000000000000000".equals(getByteStr)) {
//                    positionIndex = 6;
//                }
//                //开始尺按
//                if ("b027000000000000000000000000000000000000".equals(getByteStr)) {
//                    positionIndex = 7;
//                }
                //结束尺按
                if ("b028000000000000000000000000000000000000".equals(getByteStr)) {
//                    positionIndex = 8;
                    mark_StartEnd = 2;
                }
//                Log.d("position=====", positionIndex + "");

                String mark = ByteUtils.bytes2HexStr(new byte[]{bytes[0], bytes[1], bytes[2]});

                //总取数据
                if (mark_all1.equals(mark) || mark_all2.equals(mark) || mark_all3.equals(mark)) {
                    int cun = ((int) (((bytes[6] & 0xFF) << 8) | (bytes[7] & 0xFF)) - 1400);
                    cun = new BigDecimal(cun + "").multiply(modulus).intValue();
                    int guan = ((int) (((bytes[8] & 0xFF) << 8) | (bytes[9] & 0xFF)) - 1400);
                    guan = new BigDecimal(guan + "").multiply(modulus).intValue();
                    int chi = ((int) (((bytes[10] & 0xFF) << 8) | (bytes[11] & 0xFF)) - 1400);
                    chi = new BigDecimal(chi + "").multiply(modulus).intValue();
//                    if(cun<=0){
                    Log.d("总====cun", cun + "");
//                    }
//                    if(guan<=0){
                    Log.d("总====guan", guan + "");
//                    }
//                    if(chi<=0){
                    Log.d("总====chi", chi + "");
//                    }
                    int finalCun = cun;
                    int finalGuan = guan;
                    int finalChi = chi;
                    ThreadUtils.ui(new Runnable() {
                        @Override
                        public void run() {
                            if (finalCun >= 0) {
                                realTimeChartCun.addEntry(finalCun);
                            }
                            if (finalGuan >= 0) {
                                realTimeChartGuan.addEntry(finalGuan);
                            }
                            if (finalChi >= 0) {
                                realTimeChartChi.addEntry(finalChi);
                            }
                        }
                    });
                }
                //寸取数据
                if (mark_cun1.equals(mark) || mark_cun2.equals(mark) || mark_cun3.equals(mark)) {
                    int cun = 0;
                    //浮取
                    if (mark_cun1.equals(mark)) {
                        cun = pointUtilsCun.getDynamicPressure(bytes, PointUtils2.POSITION_CUN, PointUtils2.PRESSURE_FU);
                    }
                    //中取
                    if (mark_cun2.equals(mark)) {
                        cun = pointUtilsCun.getDynamicPressure(bytes, PointUtils2.POSITION_CUN, PointUtils2.PRESSURE_ZHONG);
                    }
                    //沉取
                    if (mark_cun3.equals(mark)) {
                        cun = pointUtilsCun.getDynamicPressure(bytes, PointUtils2.POSITION_CUN, PointUtils2.PRESSURE_CHEN);
                    }
//                    if (cun == 0) {
//                        return;
//                    }
                    int finalCun = cun;
                    ThreadUtils.ui(new Runnable() {
                        @Override
                        public void run() {
                            realTimeChartCun.addEntry(finalCun);
                        }
                    });
                }
                //关取数据
                if (mark_guan1.equals(mark) || mark_guan2.equals(mark) || mark_guan3.equals(mark)) {
                    int guan = 0;
                    //浮取
                    if (mark_guan1.equals(mark)) {
                        guan = pointUtilsGuan.getDynamicPressure(bytes, PointUtils2.POSITION_GUAN, PointUtils2.PRESSURE_FU);
                    }
                    //中取
                    if (mark_guan2.equals(mark)) {
                        guan = pointUtilsGuan.getDynamicPressure(bytes, PointUtils2.POSITION_GUAN, PointUtils2.PRESSURE_ZHONG);
                    }
                    //沉取
                    if (mark_guan3.equals(mark)) {
                        guan = pointUtilsGuan.getDynamicPressure(bytes, PointUtils2.POSITION_GUAN, PointUtils2.PRESSURE_CHEN);
                    }
//                    if (guan == 0) {
//                        return;
//                    }
                    int finalGuan = guan;
                    ThreadUtils.ui(new Runnable() {
                        @Override
                        public void run() {
                            realTimeChartGuan.addEntry(finalGuan);
                        }
                    });
                }
                //尺数据
                if (mark_chi1.equals(mark) || mark_chi2.equals(mark) || mark_chi3.equals(mark)) {

                    int chi = 0;
                    if (mark_chi1.equals(mark)) {
                        chi = pointUtilsChi.getDynamicPressure(bytes, PointUtils2.POSITION_CHI, PointUtils2.PRESSURE_FU);
                    }
                    if (mark_chi2.equals(mark)) {
                        chi = pointUtilsChi.getDynamicPressure(bytes, PointUtils2.POSITION_CHI, PointUtils2.PRESSURE_ZHONG);
                    }
                    if (mark_chi3.equals(mark)) {
                        chi = pointUtilsChi.getDynamicPressure(bytes, PointUtils2.POSITION_CHI, PointUtils2.PRESSURE_CHEN);
                    }
//                    if (chi == 0) {
//                        return;
//                    }
                    int finalChi = chi;
                    ThreadUtils.ui(new Runnable() {
                        @Override
                        public void run() {
                            realTimeChartChi.addEntry(finalChi);
                        }
                    });
                }

            } catch (Exception e) {
                e.printStackTrace();
            }


        }

        @Override
        public void onNotifySuccess(BleDevice device) {
            super.onNotifySuccess(device);
            ThreadUtils.ui(new Runnable() {
                @Override
                public void run() {
                    hasNotifyInit = true;
                    ToastUtils.showShort("Notification已初始化成功");
                    Log.d(BLETAG, "=BLE======onNotifySuccess===");
                }
            });
        }

        @Override
        public void onNotifyCanceled(BleDevice device) {
            super.onNotifyCanceled(device);
            ThreadUtils.ui(new Runnable() {
                @Override
                public void run() {
                    Log.d(BLETAG, "=BLE======onNotifyCanceled===");
                }
            });
        }
    };

    /**
     * 蓝牙数据写入方法
     *
     * @param bleDevice 连接的蓝牙设备
     * @param bytes     数据
     */
    private void writeChar(BleDevice bleDevice, byte[] bytes) {
        Ble.getInstance().writeByUuid(
                bleDevice,
                bytes,
                WriteUuidService,
                WriteUuidFeatureService,
                new BleWriteCallback<BleDevice>() {
                    @Override
                    public void onWriteSuccess(BleDevice device, BluetoothGattCharacteristic characteristic) {
                        Log.d(BLETAG, "写入特征成功=====");
                    }

                    @Override
                    public void onWriteFailed(BleDevice device, int failedCode) {
                        super.onWriteFailed(device, failedCode);
                        Log.d(BLETAG, "写入特征失败=====" + failedCode);
                    }
                });
    }

    /**
     * 蓝牙连接回调
     */
    private BleConnectCallback<BleDevice> connectCallback = new BleConnectCallback<BleDevice>() {
        @Override
        public void onConnectionChanged(BleDevice device) {
            Log.d(BLETAG, "onConnectionChanged: " + device.getConnectionState());
            if (device.isConnected()) {
                //存储已连接的蓝牙
                PreBLEConnectedData.setConnectedDeviceData(ConnectBLE2Activity.this, device.getBleAddress());
                tvBLEStatus.setText("已连接");
            } else if (device.isConnecting()) {
                tvBLEStatus.setText("连接中...");
            } else if (device.isDisconnected()) {
                tvBLEStatus.setText("未连接");
            }
        }

        @Override
        public void onConnectException(BleDevice device, int errorCode) {
            super.onConnectException(device, errorCode);
            ToastUtils.showShort("连接异常，异常状态码:" + errorCode);
        }

        @Override
        public void onConnectTimeOut(BleDevice device) {
            super.onConnectTimeOut(device);
            Log.d(BLETAG, "onConnectTimeOut: " + device.getBleAddress());
            ToastUtils.showShort("连接超时:" + device.getBleName());
        }

        @Override
        public void onConnectCancel(BleDevice device) {
            super.onConnectCancel(device);
            ToastUtils.showShort("连接取消:" + device.getBleName());
        }

        @Override
        public void onServicesDiscovered(BleDevice device, BluetoothGatt gatt) {
            super.onServicesDiscovered(device, gatt);
        }

        @Override
        public void onReady(BleDevice device) {
            super.onReady(device);
            //默认使用左手
            switchHand(1);
            //连接成功后，开启通知
            Log.d(BLETAG, "onReady==");
            enableNotifyByUuid(true);
        }
    };


//    @Override
//    protected void onStop() {
//        super.onStop();
//        closeThread();
//        sendStopToBLE();
//        ble.cancelCallback(notitifyCallback);
//        ble.cancelCallback(connectCallback);
//        if (bleDevice != null) {
//            if (bleDevice.isConnecting()) {
//                ble.cancelConnecting(bleDevice);
//            } else if (bleDevice.isConnected()) {
//                ble.disconnect(bleDevice);
//            }
//        }
//    }

    /**
     * 页面关闭时断开蓝牙通知和连接
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();
        sendStopToBLE();
        ble.cancelCallback(notitifyCallback);
        ble.cancelCallback(connectCallback);
        if (bleDevice != null) {
            if (bleDevice.isConnecting()) {
                ble.cancelConnecting(bleDevice);
            } else if (bleDevice.isConnected()) {
                ble.disconnect(bleDevice);
            }
        }
    }

    /**
     * list转int[]
     *
     * @param list
     * @return
     */
    private int[] listToArray(List<Integer> list) {
        Integer[] integers = list.toArray(new Integer[list.size()]);
        int[] data = new int[integers.length];
        for (int i = 0; i < integers.length; i++) {
            data[i] = integers[i];
        }
        return data;
    }

}
